<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        /**
         * 最大子序和
           输入：nums = [-2,1,-3,4,-1,2,1,-5,4]
           输出：6
           解释：连续子数组 [4,-1,2,1] 的和最大，为 6 。
        */
        /* 
            时间：O(n ^ 2) 该解法无法通过，会超时
            空间：O(n)
        */
        // 暴力解法
        var maxSubArray = function(nums) {
            let len = nums.length;
            // 初始化，result必须是超级小的值，防止[-1]的出现
            let result = - Infinity
            let count = 0
            for (let i = 0; i < len; i++) {
                count = 0
                // j就是要从i开始，包括他自身
                for (let j = i; j < len; j++) {
                    count += nums[j]
                    result = count > result ? count : result
                }
            }
            return result
        };
        // console.log(maxSubArray([-2,1,-3,4,-1,2,1,-5,4]));
        // console.log(maxSubArray([5,4,-1,7,8]));
        console.log(maxSubArray([-2, 1]));
    </script>
</body>

</html>